package com.yunpi.integration.common.utils;

import com.yunpi.integration.common.constants.CommonConstants;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Ret extends HashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 1L;

    public Ret() {
        put("code", CommonConstants.CODE_GET_200);
        put("msg", "操作成功");
    }

    public static Ret error() {
        return error(CommonConstants.CODE_SERVER_ERROR_500, "异常错误");
    }

    public static Ret errorAccount() {
        return error(CommonConstants.CODE_SERVER_ERROR_500, "该账号已存在");
    }

    public static Ret operate(boolean b){
        if(b){
            return Ret.ok();
        }
        return Ret.error();
    }

    public static Ret error(String msg) {
        return error(CommonConstants.CODE_SERVER_ERROR_500, msg);
    }

    public static Ret error(int code, String msg) {
        Ret r = new Ret();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static Ret ok(String msg) {
        Ret r = new Ret();
        r.put("msg", msg);
        return r;
    }

    public static Ret ok(Map<String, Object> map) {
        Ret r = new Ret();
        r.putAll(map);
        return r;
    }

    public static Ret ok() {
        return new Ret();
    }

    public static Ret error401() {
        return error(CommonConstants.CODE_UNAUTHORIZED_401, "您还没有登录!");
    }
    public static Ret error402() {
        return error(CommonConstants.CODE_INVALID_TOKEN_402, "Token已失效，请重新登录!");
    }
    public static Ret error403() {
        return error(CommonConstants.CODE_FORBIDDEN_403, "您没有此权限!");
    }
    public static Ret error404() {
        return error(CommonConstants.CODE_NOT_FOUND_404, "没有服务响应");
    }

    public static Ret data(Object data){
        return Ret.ok().put("data",data);
    }

    public static Ret page(Object page){
        return Ret.ok().put("page",page);
    }

    @Override
    public Ret put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
